package com.mbatis.leeCode;

import org.junit.Test;

import java.util.Stack;

/**
 * @Classname isValid
 * @Date 2021/5/27 15:45  有效的括号
 * @Created by zhang.yuming
 */
public class IsValid {

    public static void main(String[] args) {
        boolean valid = valid();
        System.out.println("valid-"+valid);
    }

//    @Test
    public static boolean valid(){
        String kh = "()";
        Stack<Character> stack = new Stack();

        for (int i = 0; i < kh.length(); i++) {
            char ch = kh.charAt(i);
            if (ch  == '(' || ch == '[' || ch== '{'){
                stack.push(ch);
            }else if (ch == ')' && stack.isEmpty() || stack.peek() != '('){
                return false;
            }else if(ch == ']' && stack.isEmpty() || stack.peek() != '['){
                return false;
            }else if (ch == '}'  && stack.isEmpty() || stack.peek() != '{'){
                return false;
            }
//            return stack.isEmpty();
        }
        return stack.isEmpty();
    }
}
